import uuid

import pandas as pd
import json
import numpy as np
import ast


def csv_to_json(csv_path, json_path):
    # 读取CSV文件并保留空值
    df = pd.read_csv(csv_path, keep_default_na=False)
    # 转换时间列
    for col in ['Timestamp', 'time_period']:
        if col in df.columns:
            if pd.api.types.is_datetime64_any_dtype(df[col]):
                df[col] = df[col].dt.strftime('%Y-%m-%d %H:%M:%S')
            else:
                try:
                    df[col] = pd.to_datetime(df[col]).dt.strftime('%Y-%m-%d %H:%M:%S')
                except:
                    pass

    # 构建嵌套字典结构
    result = {}
    allName = ['Topic', 'Words', 'Frequency', 'Timestamp', 'time_period', 'Representative_Docs', 'Words_gps',
               'Representative_Docs_gps']
    for _, row in df.iterrows():
        topic = str(row['Topic'])
        if topic=='-1':
            continue
        timestamp = str(row['Timestamp'])

        # 初始化嵌套结构
        result.setdefault(topic, {}).setdefault(timestamp, {})
        # 处理其他字段
        for key in row.index:
            if key not in ['Topic', 'Timestamp']:
                # 空值处理逻辑
                value = row[key]
                if (pd.isna(value) or value in ['', 'null', 'nan']) and key == "Representative_Docs":
                    result[topic][timestamp][key] = "[]"
                else:
                    result[topic][timestamp][key] = value
                if key == "Words_gps":
                    result[topic][timestamp][key] = [
                            [item[1], item[0]]  # 交换元素位置
                            for item in ast.literal_eval(value)  # 遍历子列表中的每个小列表
                        ]
                elif key == "Representative_Docs_gps":
                    result[topic][timestamp][key] = [
                        [
                            [item[1], item[0]]  # 交换元素位置
                            for item in sublist  # 遍历子列表中的每个小列表
                        ]
                        for sublist in ast.literal_eval(value)  # 遍历外层列表A的每个子列表
                    ]

        result[topic][timestamp]["id"] = str(uuid.uuid4())
    # 保存为JSON文件
    with open(json_path, 'w', encoding='utf-8') as f:
        json.dump(result, f, indent=4, ensure_ascii=False)


# 使用示例
csv_to_json('../data/topic_details/topic_over_time_GPS.csv', '../data/topic_over_time_GPS_dict1.json')
